import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'

Vue.use(Router)

const router = new Router({
    routes:[
        {path:'/',redirect:'/Login'},
        {path:'/login',component:Login},
        {path:'/home',component:Home}
    ]
})

// 挂机路由导航守卫

router.beforeEach((to,from,next) => {
    // to将要访问的路径
    // from从哪个路径来的
    // next表示放行，是一个函数
    if(to.path ==='/login') return next();
    // 如果不是login页面，则获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if(!tokenStr) return next('/login')
    next() //如果存在tokenstr，那就放行next
})

export default router